﻿using UnityEngine;
using System.Collections;

public class CameraScript : MonoBehaviour {
	
	public Transform target;
	public Transform leftSide;
	public Transform rightSide;
	
	float dampTime = 0.50f;
    private Vector3 velocity = Vector3.zero;
	private float screenLimit;
	
	// Use this for initialization
	void Start () {
		screenLimit = (float)Screen.width / Screen.height;
		screenLimit *= 6.75f;
	}
	
	// Update is called once per frame
	void FixedUpdate () {
		/*
		Vector3 tmpPos = transform.position;
		tmpPos.x = target.position.x;
		tmpPos.y = target.position.y;
		
		if(tmpPos.x < leftSide.position.x + 15)
		{
			tmpPos.x = leftSide.position.x + 15;
		}
		else if(tmpPos.x > rightSide.position.x - 15)
		{
			tmpPos.x = rightSide.position.x - 15;
		}
		
		if(tmpPos.y < 0)
		{
			tmpPos.y = 0;
		}
		*/
		
		if (target)
       	{
	        Vector3 point = camera.WorldToViewportPoint(target.position);
	        Vector3 delta = target.position - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z)); //(new Vector3(0.5, 0.5, point.z));
	        Vector3 destination = transform.position + delta;
			
			Vector3 tmpPos = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
			
			if(tmpPos.x < leftSide.position.x + screenLimit)
			{
				tmpPos.x = leftSide.position.x + screenLimit;
			}
			else if(tmpPos.x > rightSide.position.x - screenLimit)
			{
				tmpPos.x = rightSide.position.x - screenLimit;
			}
			
			if(tmpPos.y < 0.5f)
			{
				tmpPos.y = 0.5f;
			}
			
			transform.position = tmpPos;
       	}
	
		
		//transform.position = tmpPos;
	}
}
